package com.lxr0860.lxr0860production_test.itemactivity;

import static android.view.KeyEvent.KEYCODE_0;
import static android.view.KeyEvent.KEYCODE_1;
import static android.view.KeyEvent.KEYCODE_2;
import static android.view.KeyEvent.KEYCODE_3;
import static android.view.KeyEvent.KEYCODE_4;
import static android.view.KeyEvent.KEYCODE_5;
import static android.view.KeyEvent.KEYCODE_6;
import static android.view.KeyEvent.KEYCODE_7;
import static android.view.KeyEvent.KEYCODE_8;
import static android.view.KeyEvent.KEYCODE_9;
import static android.view.KeyEvent.KEYCODE_BACK;
import static android.view.KeyEvent.KEYCODE_ENTER;
import static android.view.KeyEvent.KEYCODE_MINUS;
import static android.view.KeyEvent.KEYCODE_PLUS;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.TextView;

import com.lxr0860.lxr0860production_test.R;
import com.lxr0860.lxr0860production_test.utils.Contants;
import com.lxr0860.lxr0860production_test.utils.FileUtils;
import com.lxr0860.lxr0860production_test.utils.Utils;
import com.lxr0860.lxr0860production_test.view.CustomDialog;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class KeyActivity extends Activity {
    private static final int KEYCODE_DPAD_F1 = 131;
    private static final int KEYCODE_DPAD_F2 = 132;
    private static final int KEYCODE_DPAD_SCAN1 = 139;
    private static final int KEYCODE_DPAD_SCAN2 = 138;
    private static final int KEYCODE_EDIT = 134;
    private static final int KEYCODE_MORE = 133;
    private static final int KEYCODE_LEFT = 140;
    private static final int KEYCODE_RIGHT = 141;
    private boolean keyPressR;
    private boolean keyPressL;
    private boolean keyPressScan1;
    private boolean keyPressScan2;
    private boolean keyPressMore;
    private boolean keyPressEnter;
    private boolean keyPressJian;
    private boolean keyPressEdit;
    private boolean keyPressJia;
    private boolean keyPressF1;
    private boolean keyPressF2;
    private boolean keyPressK0;
    private boolean keyPressK1;
    private boolean keyPressK2;
    private boolean keyPressK3;
    private boolean keyPressK4;
    private boolean keyPressK5;
    private boolean keyPressK6;
    private boolean keyPressK7;
    private boolean keyPressK8;
    private boolean keyPressK9;
    private boolean keyPressBack;

    private TextView mKeyRight;
    private TextView mKeyLeft;
    private TextView mKeyScan1;
    private TextView mKeyScan2;
    private TextView mKeyMore;
    private TextView mKeyEnter;
    private TextView mKeyJian;
    private TextView mKeyEdit;
    private TextView mKeyJia;
    private TextView mKeyF1;
    private TextView mKeyF2;
    private TextView mKey0;
    private TextView mKey1;
    private TextView mKey2;
    private TextView mKey3;
    private TextView mKey4;
    private TextView mKey5;
    private TextView mKey6;
    private TextView mKey7;
    private TextView mKey8;
    private TextView mKey9;
    private TextView mKeyBack;

    private boolean isBack = false;


    //    private KeyReceiver keyReceiver;
    private CustomDialog.Builder builder;
    private int testType;
    private int pressCount;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Contants.DIALOG_POSITIVE:
                    if (testType == Contants.TEST_QIANGHUA) {
                        Log.e("cly", "共按键-->" + pressCount + "次");
                        new Thread() {
                            @Override
                            public void run() {
                                FileUtils.writeFile(Contants.LOG_STRENGTH_PATH,
                                        "键盘/按键" + pressCount + "次/"
                                                + Utils.getCurrentDate() + "\r\n");
                            }
                        }.start();
                    } else {
                        Intent mIntent = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putBoolean("result", true);
                        mIntent.putExtras(bundle);
                        KeyActivity.this.setResult(Contants.TEST_ITEM_KEY, mIntent);
                    }
                    finish();
                    break;
                case Contants.DIALOG_NEGATIVE:
                    if (testType == Contants.TEST_QIANGHUA) {
                        builder = null;
                        initUI();
                    } else {
                        finish();
                    }
                    break;
            }
        }
    };

    private void initUI() {
        keyPressR = false;
        keyPressL = false;
        keyPressScan1 = false;
        keyPressScan2 = false;
        keyPressMore = false;
        keyPressEnter = false;
        keyPressJian = false;
        keyPressEdit = false;
        keyPressJia = false;
        keyPressF1 = false;
        keyPressF2 = false;
        keyPressK0 = false;
        keyPressK1 = false;
        keyPressK2 = false;
        keyPressK3 = false;
        keyPressK4 = false;
        keyPressK5 = false;
        keyPressK6 = false;
        keyPressK7 = false;
        keyPressK8 = false;
        keyPressK9 = false;
        keyPressBack = false;

        mKeyRight.setBackgroundResource(R.drawable.textview_border);
        mKeyLeft.setBackgroundResource(R.drawable.textview_border);
        mKeyScan1.setBackgroundResource(R.drawable.textview_border);
        mKeyScan2.setBackgroundResource(R.drawable.textview_border);
        mKeyMore.setBackgroundResource(R.drawable.textview_border);
        mKeyEnter.setBackgroundResource(R.drawable.textview_border);
        mKeyJian.setBackgroundResource(R.drawable.textview_border);
        mKeyEdit.setBackgroundResource(R.drawable.textview_border);
        mKeyJia.setBackgroundResource(R.drawable.textview_border);
        mKeyF1.setBackgroundResource(R.drawable.textview_border);
        mKeyF2.setBackgroundResource(R.drawable.textview_border);
        mKey0.setBackgroundResource(R.drawable.textview_border);
        mKey1.setBackgroundResource(R.drawable.textview_border);
        mKey2.setBackgroundResource(R.drawable.textview_border);
        mKey3.setBackgroundResource(R.drawable.textview_border);
        mKey4.setBackgroundResource(R.drawable.textview_border);
        mKey5.setBackgroundResource(R.drawable.textview_border);
        mKey6.setBackgroundResource(R.drawable.textview_border);
        mKey7.setBackgroundResource(R.drawable.textview_border);
        mKey8.setBackgroundResource(R.drawable.textview_border);
        mKey9.setBackgroundResource(R.drawable.textview_border);
        mKeyBack.setBackgroundResource(R.drawable.textview_border);


    }


    private void init() {
        testType = getIntent().getIntExtra("testType", Contants.TEST_ZUZHUANG);
        mKeyLeft = (TextView) findViewById(R.id.tv_key_left);
        mKeyF1 = (TextView) findViewById(R.id.tv_key_F1);
        mKeyF2 = (TextView) findViewById(R.id.tv_key_F2);
        mKeyScan1 = (TextView) findViewById(R.id.tv_key_scan1);
        mKeyScan2 = (TextView) findViewById(R.id.tv_key_scan2);
        mKeyLeft = (TextView) findViewById(R.id.tv_key_left);
        mKeyRight = (TextView) findViewById(R.id.tv_key_right);
        mKeyBack = (TextView) findViewById(R.id.tv_key_back);
        mKeyJia = (TextView) findViewById(R.id.tv_key_jia);
        mKeyJian = (TextView) findViewById(R.id.tv_key_jian);
        mKeyEdit = (TextView) findViewById(R.id.tv_key_edit);
        mKeyEnter = (TextView) findViewById(R.id.tv_key_enter);
        mKeyMore = (TextView) findViewById(R.id.tv_key_more);
        mKey0 = (TextView) findViewById(R.id.tv_key_0);
        mKey1 = (TextView) findViewById(R.id.tv_key_1);
        mKey2 = (TextView) findViewById(R.id.tv_key_2);
        mKey3 = (TextView) findViewById(R.id.tv_key_3);
        mKey4 = (TextView) findViewById(R.id.tv_key_4);
        mKey5 = (TextView) findViewById(R.id.tv_key_5);
        mKey6 = (TextView) findViewById(R.id.tv_key_6);
        mKey7 = (TextView) findViewById(R.id.tv_key_7);
        mKey8 = (TextView) findViewById(R.id.tv_key_8);
        mKey9 = (TextView) findViewById(R.id.tv_key_9);

//        if (keyReceiver == null) {
//            keyReceiver = new KeyReceiver();
//            IntentFilter mFilter = new IntentFilter("com.android.lrx.keyevent");
//            registerReceiver(keyReceiver, mFilter);
//        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_key_board);
        init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        if (keyReceiver != null) {
//            unregisterReceiver(keyReceiver);
//        }
    }

    private SystemPropertiesProxy systemPropertiesProxy = new SystemPropertiesProxy();

    @Override
    protected void onResume() {
        super.onResume();
        systemPropertiesProxy.set("persist.open.system.key.test", "open");
    }

    @Override
    protected void onPause() {
        super.onPause();
        systemPropertiesProxy.set("persist.open.system.key.test", "close");

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.e("KeyDown", keyCode + "");
        if (builder != null) {
            return true;
        }
        switch (keyCode) {
            case KEYCODE_DPAD_F1:
                switchKeyBg(mKeyF1, false);
                break;

            case KEYCODE_DPAD_F2:
                switchKeyBg(mKeyF2, false);

                break;

            case KEYCODE_DPAD_SCAN1:
                switchKeyBg(mKeyScan1, false);
                break;

            case KEYCODE_DPAD_SCAN2:

                switchKeyBg(mKeyScan2, false);
                break;
            case KEYCODE_LEFT:
                switchKeyBg(mKeyLeft, false);

                break;
            case KEYCODE_RIGHT:
                switchKeyBg(mKeyRight, false);

                break;

            case KEYCODE_BACK:

                if (testType == Contants.TEST_QIANGHUA) {
                    if (keyPressBack) {
                        if (builder == null) {
                            builder = Contants.showDailog(KeyActivity.this, "是否退出按键强化测试", handler, Gravity.TOP);
                        }
                    }
                    switchKeyBg(mKeyBack, false);

                } else {
                    switchKeyBg(mKeyBack, false);
                }
                break;

            case KEYCODE_ENTER:
                switchKeyBg(mKeyEnter, false);
                break;

            case KEYCODE_PLUS:
                switchKeyBg(mKeyJia, false);
                break;

            case KEYCODE_MINUS:
                switchKeyBg(mKeyJian, false);
                break;

            case KEYCODE_EDIT:
                switchKeyBg(mKeyEdit, false);
                break;

            case KEYCODE_MORE:
                switchKeyBg(mKeyMore, false);
                break;
            case KEYCODE_0:

                switchKeyBg(mKey0, false);
                break;

            case KEYCODE_1:

                switchKeyBg(mKey1, false);
                break;

            case KEYCODE_2:

                switchKeyBg(mKey2, false);
                break;

            case KEYCODE_3:

                switchKeyBg(mKey3, false);
                break;
            case KEYCODE_4:
                switchKeyBg(mKey4, false);
                break;
            case KEYCODE_5:

                switchKeyBg(mKey5, false);
                break;
            case KEYCODE_6:
                switchKeyBg(mKey6, false);
                break;
            case KEYCODE_7:


                switchKeyBg(mKey7, false);
                break;
            case KEYCODE_8:
                switchKeyBg(mKey8, false);
                break;
            case KEYCODE_9:
                switchKeyBg(mKey9, false);
                break;
        }
        pressCount++;

        if (keyPressR && keyPressL && keyPressScan1 && keyPressScan2 && keyPressMore && keyPressEnter
                && keyPressJian && keyPressEdit && keyPressJia && keyPressF1 && keyPressF2
                && keyPressBack
                && keyPressK0 && keyPressK1 && keyPressK2 && keyPressK3 && keyPressK4
                && keyPressK5 && keyPressK6 && keyPressK7 && keyPressK8 && keyPressK9
        ) {
            if (builder == null) {
                builder = Contants.showDailog(KeyActivity.this, "是否保存此次测试结果", handler, Gravity.TOP);
            }
        }

        return true;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        Log.e("KeyUp", keyCode + "");
        if (builder != null) {
            return true;
        }
        switch (keyCode) {
            case KEYCODE_DPAD_F1:
                keyPressF1 = true;
                switchKeyBg(mKeyF1, keyPressF1);
                break;

            case KEYCODE_DPAD_F2:

                keyPressF2 = true;
                switchKeyBg(mKeyF2, keyPressF2);


                break;

            case KEYCODE_DPAD_SCAN1:


                keyPressScan1 = true;
                switchKeyBg(mKeyScan1, keyPressScan1);


                break;

            case KEYCODE_DPAD_SCAN2:

                keyPressScan2 = true;
                switchKeyBg(mKeyScan2, keyPressScan2);

                break;
            case KEYCODE_LEFT:

                keyPressL = true;
                switchKeyBg(mKeyLeft, keyPressL);

                break;
            case KEYCODE_RIGHT:

                keyPressR = true;
                switchKeyBg(mKeyRight, keyPressR);

                break;
            case KEYCODE_BACK:
                if (testType == Contants.TEST_QIANGHUA) {

                    if (builder == null) {
                        builder = Contants.showDailog(KeyActivity.this, "是否退出按键强化测试", handler, Gravity.TOP);
                    }

                    keyPressBack = true;
                    switchKeyBg(mKeyBack, keyPressBack);


                } else {
                    if (isBack) {
                        finish();
                        isBack = false;
                        return false;
                    }
                    isBack = true;
                    keyPressBack = true;
                    switchKeyBg(mKeyBack, keyPressBack);

                }
                break;

            case KEYCODE_ENTER:


                keyPressEnter = true;
                switchKeyBg(mKeyEnter, keyPressEnter);


                break;

            case KEYCODE_PLUS:


                keyPressJia = true;
                switchKeyBg(mKeyJia, keyPressJia);


                break;

            case KEYCODE_MINUS:


                keyPressJian = true;
                switchKeyBg(mKeyJian, keyPressJian);


                break;

            case KEYCODE_EDIT:


                keyPressEdit = true;
                switchKeyBg(mKeyEdit, keyPressEdit);


                break;

            case KEYCODE_MORE:


                keyPressMore = true;
                switchKeyBg(mKeyMore, keyPressMore);


                break;
            case KEYCODE_0:


                keyPressK0 = true;
                switchKeyBg(mKey0, keyPressK0);


                break;

            case KEYCODE_1:


                keyPressK1 = true;
                switchKeyBg(mKey1, keyPressK1);


                break;

            case KEYCODE_2:


                keyPressK2 = true;
                switchKeyBg(mKey2, keyPressK2);


                break;

            case KEYCODE_3:


                keyPressK3 = true;
                switchKeyBg(mKey3, keyPressK3);


                break;
            case KEYCODE_4:


                keyPressK4 = true;
                switchKeyBg(mKey4, keyPressK4);


                break;
            case KEYCODE_5:

                keyPressK5 = true;
                switchKeyBg(mKey5, keyPressK5);


                break;
            case KEYCODE_6:


                keyPressK6 = true;
                switchKeyBg(mKey6, keyPressK6);

                break;
            case KEYCODE_7:


                keyPressK7 = true;
                switchKeyBg(mKey7, keyPressK7);


                break;
            case KEYCODE_8:


                keyPressK8 = true;
                switchKeyBg(mKey8, keyPressK8);


                break;
            case KEYCODE_9:

                keyPressK9 = true;
                switchKeyBg(mKey9, keyPressK9);

                break;
        }
        pressCount++;

        if (keyPressR && keyPressL && keyPressScan1 && keyPressScan2 && keyPressMore && keyPressEnter
                && keyPressJian && keyPressEdit && keyPressJia && keyPressF1 && keyPressF2
                && keyPressBack
                && keyPressK0 && keyPressK1 && keyPressK2 && keyPressK3 && keyPressK4
                && keyPressK5 && keyPressK6 && keyPressK7 && keyPressK8 && keyPressK9
        ) {
            if (builder == null) {
                builder = Contants.showDailog(KeyActivity.this, "是否保存此次测试结果", handler, Gravity.TOP);
            }
        }

        return true;
    }


    private void switchKeyBg(TextView textView, boolean clik) {
        if (clik) {
            textView.setBackgroundResource(R.drawable.key_press_bg_blue);
        } else {
            textView.setBackgroundResource(R.drawable.textview_border_err);
        }
    }

    private void switchKeyColor(TextView textView, boolean clik) {
        if (clik) {
            textView.setTextColor(Color.BLACK);
        } else {
            textView.setTextColor(Color.WHITE);
        }
    }

    class SystemPropertiesProxy {

        public String get(String key) {
            String result = "";
            try {
                Class<?> c = Class.forName("android.os.SystemProperties");

                Method get = c.getMethod("get", String.class);
                result = (String) get.invoke(c, key);

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            return result;
        }

        public void set(String key, String val) {
            try {
                Class<?> c = Class.forName("android.os.SystemProperties");
                Method set = c.getMethod("set", String.class, String.class);
                set.invoke(c, key, val);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

        }
    }

    private static long lastClickTime;
    private static int lastCode = -1;

    public synchronized static boolean isFastClick(int code) {
        long time = System.currentTimeMillis();
        if ((time - lastClickTime < 50) && (code != lastCode)) {
            return true;
        }
        lastCode = code;
        lastClickTime = time;
        return false;
    }

}